package com.huhu.objects;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.huhu.interfaces.IPlayer;
import com.huhu.manager.ResourcesManager;

public abstract class Player extends AnimatedSprite implements IPlayer{
	
	private Body body;
	private boolean canRun = false;
	public int footContact = 0;
	private boolean direction = true;
	private boolean die = false;
	private boolean win = false;
	private float centerX;
	private float centerY;
	private float width;
	private float height;

	public Player(float pX, float pY, VertexBufferObjectManager vbom,
			Camera camera, PhysicsWorld physicsWorld) {
		super(pX, pY, ResourcesManager.getInstance().player_region, vbom);
		
		setCurrentTileIndex(3);
		width = this.getWidth();
		height = this.getHeight();
		centerX = pX + width/2;
		centerY = pY + height/2;
		createPhysics(camera, physicsWorld);
		camera.setChaseEntity(this);
	}

	public abstract void onDie();

	private void createPhysics(final Camera camera, PhysicsWorld physicsWorld) {
		final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(0.0f,
				0.0f, 0.0f);
		body = PhysicsFactory.createBoxBody(physicsWorld, centerX, centerY, width-20, height, BodyType.DynamicBody, FIXTURE_DEF);
		body.setUserData("player");
		body.setFixedRotation(true);

		physicsWorld.registerPhysicsConnector(new PhysicsConnector(this, body,
				true, false) {
			@Override
			public void onUpdate(float pSecondsElapsed) {
				super.onUpdate(pSecondsElapsed);

				camera.onUpdate(0.1f);
				if (!die && !win) {
					if (getY() > camera.getYMax()) {
						setDie(true);
					} else {

						if (canRun) {
							if (direction) {
								body.setLinearVelocity(new Vector2(7, body
										.getLinearVelocity().y));
							} else {
								body.setLinearVelocity(new Vector2(-7, body
										.getLinearVelocity().y));
							}
						} else {
							body.setLinearVelocity(new Vector2(0, body.getLinearVelocity().y));
						}
					}
				}
				if (die) {
					setCurrentTileIndex(0);
					body.setType(BodyType.StaticBody);
					onDie();
					setIgnoreUpdate(true);
				}
				if(win){
					setCurrentTileIndex(3);
					body.setType(BodyType.StaticBody);
					setIgnoreUpdate(true);
				}
			}
		});
	}

	public void setRunning() {
		canRun = true;
		if (!die) {
			final long[] PLAYER_ANIME = { 200, 200 };
			if (direction) {
				animate(PLAYER_ANIME, 8, 9, true);
			} else {
				animate(PLAYER_ANIME, 6, 7, true);
			}
		}
	}

	public void setStopping() {
		canRun = false;
		final long[] PLAYER_ANIME = { 10000 };
		if (direction) {
			animate(PLAYER_ANIME, new int[] { 3 }, true);
		} else {
			animate(PLAYER_ANIME, new int[] { 2 }, true);

		}
	}

	public void jump() {
		if (footContact < 1) {
			return;
		} else {
			if (!ResourcesManager.getInstance().jump_sound.isPlaying()) {
				ResourcesManager.getInstance().jump_sound.play();
			}
			final long[] PLAYER_ANIME = {10000};
			if(direction){
				animate(PLAYER_ANIME, new int[]{5}, true);
			} else{
				animate(PLAYER_ANIME, new int[]{4}, true);
			}
			body.setLinearDamping(4f);
			body.setLinearVelocity(body.getLinearVelocity().x, -50f);
		}
	}
	
	public void jumpHigh(){
		if (footContact < 1) {
			return;
		} else {
			if (!ResourcesManager.getInstance().jump_sound.isPlaying()) {
				ResourcesManager.getInstance().jump_sound.play();
			}
			final long[] PLAYER_ANIME = {10000};
			if(direction){
				animate(PLAYER_ANIME, new int[]{5}, true);
			} else{
				animate(PLAYER_ANIME, new int[]{4}, true);
			}
			body.setLinearVelocity(body.getLinearVelocity().x, -50);
		}
	}
	
	public void setDirection(boolean direction) {
		this.direction = direction;
	}

	public void setDie(boolean die) {
		this.die = die;
	}
	
	public boolean isDie(){
		return this.die;
	}
	
	public void setWin(boolean win){
		this.win = win;
	}
	
	public boolean isWin(){
		return this.win;
	}

	public void increaseFootContacts() {
		footContact++;
	}

	public void decreaseFootContacts() {
		footContact--;
	}
}
